/* @(#)wrapesms.hh
 * Time-stamp: <2009-02-26 00:44:46 root>
 * Copyright 2008 Julian Qian
 * Author: root@localhost.localdomain
 * Version: $Id: wrapesms.hh,v 0.0 2008/11/08 06:01:53 root Exp $
 * Keywords: 
 */

#ifndef _WRAPESMS_H
#define _WRAPESMS_H 1

#include "soapEsmsServiceSoapProxy.h"

#include <string>
#include <algorithm>
#include <map>
#include <vector>

// using namespace std;

#define LOGIN_OK 0
#define LOGIN_EXCEPT 7

#ifndef PHONE_SIZE
#define PHONE_SIZE 14
#endif

#define SHM_MSG_SEG "msg_shared_memory"
#define SHM_PUTMSG_OBJ "put_msg_obj"
#define SHM_GETMSG_OBJ "get_msg_obj" 

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#ifndef BI
#define BI boost::interprocess
#endif

#define LEN_MOBILE 13
#define LEN_MESSAGE 70

using std::string;

struct MSGPair {
    
    char mobile[LEN_MOBILE+1];
    char message[LEN_MESSAGE+1];

    MSGPair(){
        bzero(mobile, LEN_MOBILE+1);
        bzero(message, LEN_MESSAGE+1);        
    }

    MSGPair(string mob, string msg){
        set(mob, msg);
    }

    void set(string mob, string msg){
        strncpy(mobile,  mob.c_str(), LEN_MOBILE);
        strncpy(message, msg.c_str(), LEN_MESSAGE);
    }

    string mobile_str(){
        string str(mobile);
        return str;
    }

    string message_str(){
        string str(message);
        return str;
    }
};

// typedef std::pair<std::string, std::string> MSGPair;

typedef BI::allocator <MSGPair, BI::managed_shared_memory::segment_manager> MSGShmemAllocator;

typedef BI::vector<MSGPair, MSGShmemAllocator> MsgList;

// typedef std::map<std::string, std::string> PHONE_PAIR; // l: phone number, 2: alarm info
typedef std::vector<MSGPair> MsgVector;

typedef std::string anyType_Type; // according to WSDL

class MutEX;

class Wrapesms {
public:
    bool login();
    
    static void push_msg(const char *optarg);
    static void push_msgvector(MsgVector &pp);
    static void print_msglist();
    void queue_msg(std::string mobile, std::string message);
    void putMsg();
    MsgVector* getMsg();
    void clearGetMsgResult();
    void clear();

    // singleton
    static Wrapesms* initWrapesms(MsgList *pmlist);
    static Wrapesms* theWrapesms();
    static void endWrapesms();

private:
    Wrapesms(MsgList *pmlist);
    virtual ~Wrapesms();
    
    anyType_Type _getContent(std::string &p){
        // TODO: check content
        return p;
    }

    anyType_Type _getContent(char *p){
        return p;
    }    

    int _getTaskCode() {
        if(taskCode < 65534) return taskCode++;
        else return 0;
    }

    char* _timestr();

    // variables
    static Wrapesms* _theWrapesms;
    
    std::string username;
    std::string password;
    std::string phonecode;
    std::string servcode;

    bool _disabled;

    char dateStr[20]; // "dStart"=>"2008-06-08 16:25:00"

    unsigned int taskCode; // increamental

    EsmsServiceSoapProxy *service;

    MutEX* _mutsms;
    
    MsgList *_pmlist;
    // MsgList *_gmlist;

    MsgVector _msgList;
};

#endif /* _WRAPESMS_H */

